import api from '@/api';
import { getLogin } from '@/api/admin/index';
// import Layout from '@/layout'

const state = {
  account: localStorage.account || '',
  nickName: localStorage.nickName || '',
  avatar: localStorage.avatar || '',
  token: localStorage.token || '',
  client_type: localStorage.client_type || '',
  failure_time: localStorage.failure_time || '',
  permissions: {},
  menu: localStorage.menus || ''
}

const getters = {
  isLogin: state => {
    let retn = false;
    // console.log('????-- isLogin');
    if (state.token) {
      let unix = Date.parse(new Date())
      // console.log(unix, '当前时间戳?????', state.failure_time);
      if (unix < state.failure_time * 1000) {
        retn = true
      }
    }
    return retn
  },
  getMenu: state => {
    const menu = state.menu
    return menu
  },
  getPermission: state => {
    //   console.log(state.permissions, '<--- vuex -user state.permissions');
    return state.permissions
  }
}

const actions = {
  login ({ commit }, data) {
    return new Promise((resolve, reject) => {
      // 通过 mock 进行登录
      // api.post('mock/member/login', data).then(res => {
      //   commit('setUserData', res.data)
      //   resolve()
      // }).catch(error => {
      //   reject(error)
      // })

      getLogin(data).then(response => {
        console.log('response: ', response);
        commit('setUserData', response?.data ?? {});
        resolve();
      }).catch(error => {
        reject(error);
      })
    })
  },
  logout ({ commit }) {
    commit('removeUserData')
    commit('menu/invalidRoutes', null, { root: true })
  },
  // 获取我的权限
  getPermissions ({ state, commit }) {
    return new Promise(resolve => {
      // 通过 mock 获取权限
      api.get('mock/member/permission', {
          params: {
              account: state.account
          }
      }).then(res => {
          commit('setPermissions', res.data.permissions)
          resolve(res.data.permissions)
      })
  })
    // return new Promise((resolve, reject) => {
    //   api.get('/tube/admin/indo').then(response => {
    //     commit('SET_PERMISSIONS', response.data || []);
    //     resolve();
    //   }).catch(error => {
    //     reject(error);
    //   });
    // })
  },
  // 获取我的菜单
  getMenus ({ commit }) {
    return new Promise((resolve, reject) => {
      // 通过 mock 获取权限
      api.get('tube/routesAdmin', null
      ).then(res => {
        commit('setMenus', res.data)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

const mutations = {
  setUserData (state, data) {
    localStorage.setItem('account', data?.account??'');
    localStorage.setItem('token', data?.token??'');
    localStorage.setItem('failure_time', data?.failure_time??'');
    state.account = data?.account??'';
    state.token = data?.token??'';
    state.failure_time = data?.failure_time??'';
    // localStorage.setItem('account', data.account);
    // localStorage.setItem('nickName', data.nickName);
    // localStorage.setItem('avatar', data.avatar);
    // localStorage.setItem('token', data.token);
    // localStorage.setItem('client_type', 'PC ' + data.idCode);
    // localStorage.setItem('failure_time', data.failure_time);
    // state.account = data.account;
    // state.nickName = data.nickName;
    // state.avatar = data.avatar;
    // state.token = data.token;
    // state.client_type = 'PC ' + data.idCode;
    // state.failure_time = data.failure_time;
  },
  removeUserData (state) {
    localStorage.removeItem('account');
    localStorage.removeItem('token');
    localStorage.removeItem('client_type');
    localStorage.removeItem('failure_time');
    localStorage.removeItem('menus');
    state.account = '';
    state.token = '';
    state.client_type = '';
    state.failure_time = '';
    state.menus = '';
  },
  setPermissions(state, permissions) {
      state.permissions = permissions
  },
  setMenus (state, menus) {
    localStorage.setItem('menus', JSON.stringify(menus))
    state.menu = JSON.stringify(menus)
  },
  SET_PERMISSIONS: (state, permissions) => {
    const list = {}
    for (let i = 0; i < permissions.length; i++) {
      list[permissions[i]] = true
    }
    state.permissions = list
  }
}

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}
